﻿using AntDesign;
using AntDesign.TableModels;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text.Json;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Components.Web.Extensibility.EntityActions;
using Volo.Abp.AspNetCore.Components.Web.Extensibility.TableColumns;


namespace ERP.Abp.AntDesignUI
{
    public partial class AdminTable<TItem> : ComponentBase
    {

        protected const string DataFieldAttributeName = "Data";

        protected Dictionary<string, TableEntityActionsColumn<TItem>> ActionColumns = new();


        [Inject] IJSRuntime IJSRuntime { get; set; }
        [Parameter] public IEnumerable<TItem> DataSource { get; set; }
        [Parameter] public IEnumerable<TItem> SelectedRows { get; set; }


        [Parameter] public IEnumerable<CustTableColumn> Columns { get; set; }
        [Parameter] public int Total { get; set; } = 0;
        [Parameter] public int PageSize { get; set; } = 20;
        [Parameter] public int PageIndex { get; set; } = 1;
        [Parameter] public string ScrollY { get; set; } = "600px";
        [Parameter] public bool Loading { get; set; }
        [Parameter] public bool RemoteDataSource { get; set; }
        [Parameter] public RenderFragment SearchTemplate { get; set; }


        [Parameter] public RenderFragment<AdminTable<TItem>> ToolbarTemplate { get; set; }
        [Parameter] public RenderFragment<AdminTable<TItem>> ToolbarLeftTitleTemplate { get; set; }
        [Parameter] public RenderFragment<AdminTable<TItem>> ToolbarRightButtonTemplate { get; set; }
        [Parameter] public RenderFragment<AdminTable<TItem>> ColumnsTemplate { get; set; }
        [Parameter] public EventCallback<QueryModel<TItem>> OnChange { get; set; }


        ITable table;
        string _guid = $"table{DateTime.Now.Ticks}";
        [Parameter] public bool ExpandSearchBox { get; set; }

        protected override Task OnInitializedAsync()
        {
            Total = 100;
            return Task.CompletedTask;
        }
        protected override void OnAfterRender(bool firstRender)
        {

            base.OnAfterRender(firstRender);
            if (firstRender)
            {

            }

        }
        public override Task SetParametersAsync(ParameterView parameters)
        {
            return base.SetParametersAsync(parameters);
        }

        public void RemoveSelection(int id)
        {

        }

        private void Delete(int id)
        {

        }

        protected override void OnInitialized()
        {
            base.OnInitialized();
        }



        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            base.OnAfterRender(firstRender);
            if (firstRender)
            {
                await ResetTableHeight();
            }
        }



        public async Task ResetTableHeight()
        {
            var height1 = await IJSRuntime.InvokeAsync<int>("dom.getDom", $".ant-tabs-nav-wrap", "offsetHeight");
            var height2 = await IJSRuntime.InvokeAsync<int>("dom.getDom", $"#{_guid} .search", "offsetHeight");
            var height3 = await IJSRuntime.InvokeAsync<int>("dom.getDom", $"#{_guid} .toolbar", "offsetHeight");
            var height4 = await IJSRuntime.InvokeAsync<int>("dom.getDom", $"#{_guid} table>thead", "offsetHeight");
            var height5 = await IJSRuntime.InvokeAsync<int>("dom.getDom", $"#{_guid} .ant-table-pagination", "offsetHeight");
            var totals = await IJSRuntime.InvokeAsync<int>("dom.getDom", "body", "offsetHeight");
            ScrollY = (totals - height1 - height2 - height3 - height4 - height5 - 48 - 10 - 8 * 2 - 4) + "px";
            StateHasChanged();
        }
        class FormValue
        {
            public string Value { get; set; }
        }







        protected virtual RenderFragment RenderCustomTableColumnComponent(Type type, object data)
        {
            return (builder) =>
            {
                builder.OpenComponent(0, type);
                builder.AddAttribute(0, DataFieldAttributeName, data);
                builder.CloseComponent();
            };
        }

        protected virtual object GetColumnValue(object data, string fieldName)
        {
            return data.GetType().GetProperty(fieldName)?.GetValue(data);
        }

        protected virtual string GetConvertedFieldValue(TItem item, CustTableColumn columnDefinition)
        {
            var convertedValue = columnDefinition.ValueConverter.Invoke(item);
            if (!columnDefinition.DisplayFormat.IsNullOrEmpty())
            {
                return string.Format(columnDefinition.DisplayFormatProvider, columnDefinition.DisplayFormat,
                    convertedValue);
            }

            return convertedValue;
        }

        public void ToggleExpandSearchBox ()
        {
            ExpandSearchBox = !ExpandSearchBox;
        }




    }
}
